<?php
class RecordsView
{
	public static function getViewByType($type)
	{
		$view = false;
		switch($type)
		{
			case RecordsHelper::$TYPE_TODAY:
				$view = new RecordsViewDay();
			break;
			case RecordsHelper::$TYPE_THREE_DAY:
				$view = new RecordsViewThreeDay();
			break;
			case RecordsHelper::$TYPE_WEEK:
				$view = new RecordsViewWeek();
			break;
			case RecordsHelper::$TYPE_MONTH:
				$view = new RecordsViewMonth();
			break;
			default:
				throw new Exception('unsupported type for initial records view');
		}
		return $view;
	}
}

abstract class RecordsViewBase
{
	protected $dataSource;
	
		protected $weekdayMap = array(
	1=>'星期一',
	2=>'星期二',
	3=>'星期三',
	4=>'星期四',
	5=>'星期五',
	6=>'星期六',
	7=>'星期日'
	);
	public function setDataSource($dataSource)
	{
		$this->dataSource = $dataSource;
	}
    public function display()
    {
    	if(!is_array($this->dataSource) && count($this->dataSource) > 0)
    		return;
    	$this->headerView();
    	$totalRecordCount = count($this->dataSource);
    	foreach($this->dataSource as $index=>$itmeData)
    	{

    		$this->itemView($itmeData);
    		if($index < $totalRecordCount - 1)
    			$this->itemRecordSeperator();
    	}
    	$this->footerView();
    }
    protected function itemRecordSeperator()
    {
    	echo "<li style='clear:both;background-color:#333333;height:1px;width:100%;'></li>";
    }
    
	abstract protected function headerView();
    abstract protected function itemView($itemData);
    abstract protected function footerView();
}
class RecordsViewDay extends RecordsViewBase
{
	protected function headerView()
	{
		printf("<strong>%s</strong><br>", count($this->dataSource) > 0?$this->dataSource[0]['date']:'');
		echo "<hr>";
		
		
		echo "<ul class='ullist'>";
	}
    protected function itemView($itemData)
    {
    		printf("<li class='label1'>录入账号</li><li class='label2'>%s</li>",$itemData['createInfo']['User']);
    		echo "<li style='clear:both;'></li>";
    		
    	if($itemData['type'] == constant('RECORD_TYPE_NORMAL_PAY'))
    	{

    	
    		
    		printf("<li class='label1'>类型</li><li class='label2'>%s</li>",'一般支付');
    		
    		echo "<li style='clear:both;'></li>";
    		
    		printf("<li class='label1'>总计</li><li class='label2'>%s</li>",$itemData['total']);
    	echo "<li style='clear:both;'></li>";
    		printf("<li class='label1'>详细</li><li class='label2'>%s</li>",$itemData['detail']);
    	echo "<li style='clear:both;'></li>";
    		
    		$assignInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['assign'] as $key=>$value)
    		{
    			$assignInfo .= sprintf("<tr><td>%s</td><td>%s</td></tr>",$key,$value);
    		}
    		$assignInfo .= '</table>';
    		
    		printf("<li class='label1'>分配</li><li class='label2'>%s</li>",$assignInfo);
    		echo "<li style='clear:both;'></li>";
    		
    		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		
    		printf("<li class='label1'>余额</li><li class='label2'>%s</li>",$balanceInfo);
    		
    		
    	}
    	
    	if($itemData['type'] == constant('RECORD_TYPE_TRANS'))
    	{
    		printf("<li class='label1'>类型</li><li class='label2'>%s</li>",'转账');
    		echo "<li style='clear:both;'></li>";
    		
    		printf("<li class='label1'>总计</li><li class='label2'>%s</li>",$itemData['total']);
    		echo "<li style='clear:both;'></li>";
    		
    		printf("<li class='label1'>详细</li><li class='label2'>%s => %s</li>",$itemData['from'],$itemData['to']);
    		echo "<li style='clear:both;'></li>";
    		
    		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		printf("<li class='label1'>余额</li><li class='label2'>%s</li>",$balanceInfo);
    		
    		
    	}
    	
    }
    protected function footerView()
    {
    	echo "</ul>";
    }
}
class RecordsViewThreeDay extends RecordsViewBase
{
	private $firstDay;
	private $secondDay;
	private $thirdDay;
	
	private $html_firstDay;
	private $html_secondDay;
	private $html_thirdDay;
	
	private $firstDay_add = false;
	private $secondDay_add = false;
	private $thirdDay_add = false;
	protected function headerView()
	{

		$currentTime = AppMain::getCurrentTime();
		$t_currentTime = strtotime($currentTime);
		$this->thirdDay =  date('Y-m-d',$t_currentTime);
		$this->secondDay = date('Y-m-d',strtotime("-1 day",$t_currentTime));
		$this->firstDay = date('Y-m-d',strtotime("-2 day",$t_currentTime));
			
		
		$this->html_firstDay = '<div class="threedaydiv">'.sprintf("<ul class='ullist'><strong>%s</strong><br>", $this->firstDay);
		$this->html_secondDay = '<div class="threedaydiv">'.sprintf("<ul class='ullist'><strong>%s</strong><br>", $this->secondDay);
		$this->html_thirdDay = '<div class="threedaydiv">'.sprintf("<ul class='ullist'><strong>%s</strong><br>", $this->thirdDay);
	}
    protected function itemView($itemData)
    {
    	 $this->currentIterateIndex++;
    	$html = '';
    if($itemData['type'] == constant('RECORD_TYPE_NORMAL_PAY'))
    {
    
    	   	$html .= sprintf("<li class='label1'>录入账号</li><li class='label2'>%s</li>",$itemData['createInfo']['User']);
			$html .= "<li style='clear:both;'></li>";
			
    		$html .= sprintf("<li class='label1'>类型</li><li class='label2'>%s</li>",'一般支付');
    		
    		$html .= "<li style='clear:both;'></li>";
    		
    		$html .= sprintf("<li class='label1'>总计</li><li class='label2'>%s</li>",$itemData['total']);
    	$html .= "<li style='clear:both;'></li>";
    		$html .= sprintf("<li class='label1'>详细</li><li class='label2'>%s</li>",$itemData['detail']);
    	$html .= "<li style='clear:both;'></li>";
    		
    		$assignInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['assign'] as $key=>$value)
    		{
    			$assignInfo .= sprintf("<tr><td>%s</td><td>%s</td></tr>",$key,$value);
    		}
    		$assignInfo .= '</table>';
    		
    		$html .= sprintf("<li class='label1'>分配</li><li class='label2'>%s</li>",$assignInfo);
    		$html .=  "<li style='clear:both;'></li>";
    		
    		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		
    		$html .= sprintf("<li class='label1'>余额</li><li class='label2'>%s</li>",$balanceInfo);

    	
    	}else if($itemData['type'] == constant('RECORD_TYPE_TRANS'))
    	{
    		$html .= sprintf("<li class='label1'>录入账号</li><li class='label2'>%s</li>",$itemData['createInfo']['User']);
			$html .= "<li style='clear:both;'></li>";
			
    			$html .= sprintf("<li class='label1'>类型</li><li class='label2'>%s</li>",'转账');
    		$html .= "<li style='clear:both;'></li>";
    		
    		$html .= sprintf("<li class='label1'>总计</li><li class='label2'>%s</li>",$itemData['total']);
    		$html .= "<li style='clear:both;'></li>";
    		
    		$html .= sprintf("<li class='label1'>详细</li><li class='label2'>%s => %s</li>",$itemData['from'],$itemData['to']);
    		$html .= "<li style='clear:both;'></li>";
    		
    		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		$html .= sprintf("<li class='label1'>余额</li><li class='label2'>%s</li>",$balanceInfo);

    	}
    	if($itemData['date'] == $this->firstDay)
    	{
    		if( $this->firstDay_add === true)
    			$html = "<li style='clear:both;background-color:#333333;height:1px;'></li>".$html;
    		$this->html_firstDay .= $html;
    		$this->firstDay_add = true;
    	}else if($itemData['date'] == $this->secondDay)
    	{
    		if( $this->secondDay_add === true)
    			$html = "<li style='clear:both;background-color:#333333;height:1px;'></li>".$html;
    		$this->html_secondDay .= $html;
    		$this->secondDay_add = true;
    	}else if($itemData['date'] == $this->thirdDay)
    	{
    		if( $this->thirdDay_add === true)
    			$html = "<li style='clear:both;background-color:#333333;height:1px;'></li>".$html;
    		$this->html_thirdDay .= $html;
    		$this->thirdDay_add = true;
    	}
    }
    protected function footerView()
    {
    	$this->html_firstDay .= '</ul></div>';
		$this->html_secondDay .= '</ul></div>';
		$this->html_thirdDay .= '</ul></div>';
		
		echo $this->html_firstDay;
		echo $this->html_secondDay;
		echo $this->html_thirdDay;
    }
    
    protected function itemRecordSeperator()
    {
    	
    }
}
class RecordsViewWeek extends RecordsViewBase
{

	
	private $lastDay = false;
	protected function headerView()
	{
		echo "<ul class='weekdatatable'>";
		echo '<li class="th">账号</li>';
		echo '<li class="th">类型</li>';
				echo '<li class="th">总计</li>';
			echo '<li class="th" style="width:200;">详细</li>';
			echo '<li class="th" style="width:180;">分配</li>';
			echo '<li class="th" style="width:180;">余额</li>';
			
			

	}
    protected function itemView($itemData)
    {
    $dateObj = strtotime($itemData['date']);
    $dayOfWeek = $this->weekdayMap[date('N',$dateObj)];
    if($dayOfWeek != $this->lastDay)
    {
    echo "<li style='clear:both;background-color:#3366ee;height:20px;width:100%;color:white;padding-left:8px;'>".$dayOfWeek."</li>";
    $this->lastDay = $dayOfWeek;
    }else{
   // echo "<li style='clear:both;background-color:#333333;height:1px;width:100%'></li>";
    }
    
    
     printf("<li >%s</li>",$itemData['createInfo']['User']);
   
    		
    	if($itemData['type'] == constant('RECORD_TYPE_NORMAL_PAY'))
    	{
    	echo '<li >一般支付</li>';
		echo '<li >'.$itemData['total'].'</li>';
		echo '<li style="width:200;">'.$itemData['detail'].'</li>';
		
			$assignInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['assign'] as $key=>$value)
    		{
    			$assignInfo .= sprintf("<tr><td>%s</td><td>%s</td></tr>",$key,$value);
    		}
    		$assignInfo .= '</table>';
    		
		echo '<li style="width:180;">'.$assignInfo.'</li>';
		
		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		
		echo '<li style="width:180;">'.$balanceInfo.'</li>';	
    	}else{
    	echo '<li >转账</li>';
		echo '<li >'.$itemData['total'].'</li>';
		echo '<li style="width:200;">'.sprintf("%s => %s",$itemData['from'],$itemData['to']).'</li>';
		
		
    		
		echo '<li style="width:180;">&nbsp;</li>';
		
		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		
		echo '<li style="width:180;">'.$balanceInfo.'</li>';
    	}
    	
    	//echo "<li style='clear:both;background-color:#cccccc;height:1px;width:100%'></li>";
    }
    protected function footerView()
    {
    	echo '</ul>';
    }
}
class RecordsViewMonth extends RecordsViewBase
{
	
	private $lastDay = false;
	private $totalAmount = 0;
	protected function headerView()
	{
		echo "<div><form type='get'><input type='hidden' name='type' value='month'/>";
		
		$currentTime = AppMain::getCurrentTime();
		
		$t_currentTime = strtotime($currentTime);
		$fYear = isset($_REQUEST['fYear'])?$_REQUEST['fYear']:date('Y',$t_currentTime);
		$fMonth = isset($_REQUEST['fMonth'])?$_REQUEST['fMonth']:date('n',$t_currentTime);
		
		echo "<select name='fYear' onchange='this.form.submit();'>";

		for($i=2012; $i <= $fYear; $i++)
		{
			if($i == $fYear)
			{
				printf("<option value='%d' selected='selected'>%d</option>",$i,$i);
			}else{
				printf("<option value='%d'>%d</option>",$i,$i);
			}
		}
		echo "</select>";
		echo "&nbsp;&nbsp;";
		echo "<select name='fMonth' onchange='this.form.submit();'>";
		for($i=1; $i < 12; $i++)
		{
			if($i < 10)
				$i = '0'.$i;
			if($i == $fMonth)
			{
				printf("<option value='%s' selected='selected'>%s</option>",$i,$i);
			}else{
				printf("<option value='%s'>%s</option>",$i,$i);
			}
			
		}
		echo "</select></form>";
		
		echo "</div>";
		echo "<ul class='weekdatatable'>";
			echo '<li class="th">账号</li>';
		echo '<li class="th">类型</li>';
				echo '<li class="th">总计</li>';
			echo '<li class="th" style="width:200;">详细</li>';
			echo '<li class="th" style="width:180;">分配</li>';
			echo '<li class="th" style="width:180;">余额</li>';
			
			

	}
    protected function itemView($itemData)
    {
    $dateObj = strtotime($itemData['date']);
    $dayOfWeek = $this->weekdayMap[date('N',$dateObj)];
    if($dayOfWeek != $this->lastDay)
    {
    echo "<li style='clear:both;background-color:#3366ee;height:20px;width:100%;color:white;padding-left:8px;'>".$dayOfWeek."   ".$itemData['date']."</li>";
    $this->lastDay = $dayOfWeek;
    }else{
   // echo "<li style='clear:both;background-color:#333333;height:1px;width:100%'></li>";
    }
    printf("<li >%s</li>",array_key_exists('createInfo',$itemData)?$itemData['createInfo']['User']:'');
    	if($itemData['type'] == constant('RECORD_TYPE_NORMAL_PAY'))
    	{
    	echo '<li >一般支付</li>';
		echo '<li >'.$itemData['total'].'</li>';
		echo '<li style="width:200;">'.$itemData['detail'].'</li>';
		
			$assignInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['assign'] as $key=>$value)
    		{
    			$assignInfo .= sprintf("<tr><td>%s</td><td>%s</td></tr>",$key,$value);
    		}
    		$assignInfo .= '</table>';
    		
		echo '<li style="width:180;">'.$assignInfo.'</li>';
		
		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		
		echo '<li style="width:180;">'.$balanceInfo.'</li>';
		
		$this->totalAmount += 	$itemData['total'];
    	}else{
    	echo '<li >转账</li>';
		echo '<li >'.$itemData['total'].'</li>';
		echo '<li style="width:200;">'.sprintf("%s => %s",$itemData['from'],$itemData['to']).'</li>';
		
		
    		
		echo '<li style="width:180;">&nbsp;</li>';
		
		$balanceInfo = '<table cellpadding="8"><tr><th>账户</th><th>金额</th></tr>';
    		foreach($itemData['balanceInfo'] as $key=>$value)
    		{
    			$color = '#000000';
    			if($value < 0)
    				$color = 'green';
    			$balanceInfo .= sprintf("<tr><td>%s</td><td style='color:%s'>%s</td></tr>",$key,$color,$value);
    		}
    		$balanceInfo .= '</table>';
    		
		echo '<li style="width:180;">'.$balanceInfo.'</li>';
    	}
    	
    //	echo "<li style='clear:both;background-color:#cccccc;height:1px;width:100%'></li>";
    }
    protected function footerView()
    {
    	echo '</ul>';
    	
    	echo "<li style='clear:both;'></li>";
    	echo "<hr>";
    	printf("<div style='font-size:20px;color:red;'><span>总计：</span><span>%0.2f</span></div>",$this->totalAmount);
    }
}